import { constantRoutes, asyncRoutes } from '@/router'
const state = {
  routes: constantRoutes // 路由表,默认付给静态路由表
}
const mutations = {
  setRoutes(state, newRoutes) {
    // 需在原有静态路由上解构，不应该在state.routes上解构
    // 因为退出时可能上一用户登录的权限还在
    state.routes = [...constantRoutes, ...newRoutes]
  }
}
const actions = {
  filterRoutes(context, menus) {
    const routes = []
    menus.forEach(key => {
      // asyncRoutes筛选返回一个数组 []里面push[] 可以先把push的数组解构再push
      routes.push(...asyncRoutes.filter(item => item.children[0].name === key))
    })
    context.commit('setRoutes', routes)
    return routes // state数据是用来显示左侧菜单的 return是用来给路由addRoute用的
  }
}
export default {
  namespaced: true,
  state,
  mutations,
  actions
}
